﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Web.Support;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recDevolutionProcessform : SIPO.Web.BasePage
    {
        int id = 0;
        string aksi = string.Empty;

        protected override void OnLoad(EventArgs e)
        {
            if(Request.Params["DevolutionProcessDetailID"] != null){
               id = int.Parse(Request.Params["DevolutionProcessDetailID"].ToString());
           }

           if (Request.Params["aksi"] != null)
           {
               aksi = Request.Params["aksi"].ToString();
           }

           if (!IsPostBack)
           {
               if (id > 0)
               {
                   //get recDevolutionAction
                   recDevolutionActionRepo recDevolutionActionDal = new recDevolutionActionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                   List<recDevolutionAction> recDevolutionActions = null;
                   //get recDevolutionProcess
                   recDevolutionRepo recDevolutionDal = new recDevolutionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                   string cekLastasDevolutionAction = recDevolutionDal.cekLastDevolutionAction(id);

                   if (cekLastasDevolutionAction.Equals(string.Empty))
                   {
                       //ambil list recDevolutionAction
                       recDevolutionActions = recDevolutionActionDal.GetList();
                   }
                   else
                   {
                       if (aksi.Equals("update"))
                       {
                           recDevolutionActions = recDevolutionActionDal.GetList();
                       }
                       else
                       {
                           recDevolutionActions = recDevolutionDal.GetListDevolutionAction(id);
                       }
                       
                   }

                   if (recDevolutionActions != null)
                   {
                       ddlPengurusan.Items.Clear();
                       recDevolutionActions.ForEach(delegate(recDevolutionAction devolutionAction)
                       {
                           ddlPengurusan.Items.Add(new ListItem(devolutionAction.DevolutionCode + '-' + devolutionAction.DevolutionName, devolutionAction.DevolutionID.ToString()));
                       });
                       ddlPengurusan.Items.Insert(0, new ListItem("-- pilih Tahap Pengurusan  --", string.Empty));
                   }

                   
                   recDevolutionProcess recDevolutionProcess = recDevolutionDal.GetByIdDevolutionProcess(id, cekLastasDevolutionAction);

                   txtDebtorName.Text = recDevolutionProcess.DebtorName;
                   txtDebtorId.Text = recDevolutionProcess.DebtorID.ToString();
                   txtLetterNo.Text = recDevolutionProcess.LetterDevolutionNo;
                   txtLetterDate.Text = recDevolutionProcess.LetterDevolutionDate.Value.ToString("dd/MM/yyyy");
                   txtDevolutionDate.Text = recDevolutionProcess.DevolutionDate.Value.ToString("dd/MM/yyyy");

                   hfLastDevalutionAction.Value = recDevolutionProcess.recDevolutionAction.DevolutionID.ToString();
                   hfDevolutionProcessDetailId.Value = id.ToString();
                   hfDevolutionProcessId.Value = recDevolutionProcess.DevolutionProcessID.ToString();
                   hfAksi.Value = aksi;

                   // jika aksinya update
                   if(aksi.Equals("update"))
                   {
                       ddlPengurusan.Items.FindByValue(recDevolutionProcess.recDevolutionAction.DevolutionID.ToString()).Selected = true;
                       hfDevolutionId.Value = recDevolutionProcess.recDevolutionAction.DevolutionID.ToString();
                       txtNoSuratPengurusan.Text = recDevolutionProcess.recDevolutionProcessDetail.LetterDevProcessDetailNo;
                       txtTglSuratPengurusan.Text = recDevolutionProcess.recDevolutionProcessDetail.LetterDevProcessDetailDate.Value.ToString("dd/MM/yyyy");
                   }
               }
           }


        }
    }
}